package com.mls.fivedistrict.controller.portal;

import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.enums.RecordStatusEnum;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Record;
import com.mls.fivedistrict.pojo.User;
import com.mls.fivedistrict.server.PropertyService;
import com.mls.fivedistrict.server.RecordService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created by nanji on 2018/3/16.
 */
@RestController
@RequestMapping("/api/app")
public class RecordController extends BaseController {
    @Resource(name = "recordService")
    private RecordService recordService;

    @Resource(name = "propertyService")
    private PropertyService propertyService;


    @PutMapping(path = "/v1/record/{recordId}")
    public ServiceResponse editRecord(@PathVariable Long recordId, @RequestBody Record record) {
        Optional.ofNullable(recordId).orElseThrow(() -> new CustomerException("记录不存在"));
        record.setId(recordId);
        return recordService.editRecord(record);
    }

    @GetMapping(path = "/v1/records/old")
    public ServiceResponse getRecordList() {
        PageFilter pageFilter = getPageFilter();
        pageFilter.getFilterMap().put("userId", getUser().getId() + "");
        List<Record> recordListSingle = recordService.getRecordListSingle(pageFilter);
        PageInfo<Record> recordPageInfo = new PageInfo<>(recordListSingle, 5);
        return ServiceResponse.createSuccessResponse(recordPageInfo);
    }

    @GetMapping(path = "/v1/records/waiting")
    public ServiceResponse getRecordListWaiting() {
        PageFilter pageFilter = getPageFilter();
        Map<String, String> filterMap = pageFilter.getFilterMap();
        filterMap.put("status", RecordStatusEnum.WAITRECEIVE.getValue());
        User user = getUser();
        filterMap.put("userId", user.getId() + "");
        return recordService.getRecordListByApp(pageFilter);
    }

    /**
     * 用户领取积分
     *
     * @param recordId 记录ID
     * @return
     */
    @PutMapping(path = "/v1/record/accept/{recordId}")
    public ServiceResponse acceptIntegral(@PathVariable Long recordId) {
        Optional.ofNullable(recordId).orElseThrow(() -> new CustomerException("记录不存在"));

        int i = recordService.acceptRecord(recordId, getUser().getId());
        if (i > 0) {
            return ServiceResponse.createSuccessResponse();
        } else {
            return ServiceResponse.createErrorResponse("领取失败");
        }
    }


    @GetMapping(path = "/v1/record/receive/sum")
    public ServiceResponse getReceiveCount() {
        BigDecimal recordSum = recordService.getRecordSum();
        return ServiceResponse.createSuccessResponse(recordSum);

    }
}
